<template>
  <main-container>
    <div class="user-create-page">
      <el-breadcrumb separator="/" class="mb30">
        <el-breadcrumb-item :to="{ name: 'userList' }">管理员设置</el-breadcrumb-item>
        <el-breadcrumb-item>添加管理员</el-breadcrumb-item>
      </el-breadcrumb>
      <div class="form-wrapper">
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="140px">
          <el-form-item label="用户名：" prop="realName">
            <el-input v-model="ruleForm.realName" :maxlength="10" clearable placeholder="请输入用户名"></el-input>
          </el-form-item>
          <el-form-item label="手机号：" prop="phone">
            <el-input v-model="ruleForm.phone" :maxlength="11" clearable placeholder="请输入手机号"></el-input>
          </el-form-item>
          <el-form-item label="密码：" prop="password">
            <el-input v-model="ruleForm.password" :maxlength="32" clearable placeholder="请输入密码"></el-input>
          </el-form-item>
          <el-form-item label="角色选择：" prop="roleId">
            <el-select class="w-fill" v-model="ruleForm.roleId" placeholder="请选择角色">
              <el-option v-for="item in roleList" :key="item.value" :label="item.roleName" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" class="w100" :loading="loading" @click="handleSubmit">确定</el-button>
            <router-link :to="{ name: 'userList' }" class="ml20">
              <el-button class="w100">取消</el-button>
            </router-link>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </main-container>
</template>

<script>
import { getRoleList, createUserInfo } from '@/api/workbench'
import { isChinese, isPhone, isNumLetter } from '@/utils/regs'

export default {
  name: 'userCreate',
  data() {
    let validateRealName = (rule, value, callback) => {
      if (!isChinese(value)) {
        callback(new Error('用户名只能输入中文'))
      } else {
        callback()
      }
    }
    let validatePhone = (rule, value, callback) => {
      if (!isPhone(value)) {
        callback(new Error('请输入正确的手机号'))
      } else {
        callback()
      }
    }
    let validatePassword = (rule, value, callback) => {
      if (!isNumLetter(value)) {
        callback(new Error('密码只能输入数字和字母'))
      } else {
        callback()
      }
    }
    return {
      loading: false,
      ruleForm: {
        realName: '',
        phone: '',
        password: '',
        roleId: ''
      },
      rules: {
        realName: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { validator: validateRealName, trigger: 'blur' }
        ],
        phone: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { validator: validatePhone, trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 32, message: '密码长度在 6 到 32 个字符', trigger: 'blur' },
          { validator: validatePassword, trigger: 'blur' }
        ],
        roleId: [{ required: true, message: '请选择角色', trigger: 'change' }]
      },
      roleList: []
    }
  },
  created() {
    this.getRoleList()
  },
  methods: {
    /**
     * 角色列表
     */
    async getRoleList() {
      let res = await getRoleList({ pageNum: 1, pageSize: 999999 })
      let { code, data } = res
      if (code === 0) {
        let { roleList } = data
        this.roleList = roleList
      }
    },
    handleSubmit() {
      this.$refs.ruleForm.validate(async valid => {
        if (valid) {
          this.loading = true
          let res = await createUserInfo(this.ruleForm).catch(() => (this.loading = false))
          this.loading = false
          let { code, msg: message } = res
          if (code === 0) {
            this.$message.success(message)
            this.$router.go(-1)
          }
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
@import './index.scss';
</style>
